<?php

//power by wshoto.com
global $_W, $_GPC;
$openid = m('user')->getOpenid();

$tabwidth = "50";

if ($this->set['level'] >= 1) {
	$tabwidth = 100;
}
if ($this->set['level'] >= 2) {
	$tabwidth = 50;
}
if ($this->set['level'] >= 3) {
	$tabwidth = 33.3;
}

$member = m('member')->getMember($openid);

$agentTeamsCount = $this->model->getCommissionTeamCount($openid, $this->set['level']);

$total = $member['agentcount'] = number_format($agentTeamsCount['all'], 0);

$level1 = !empty($agentTeamsCount[1]) ? $agentTeamsCount[1] : 0;
$level2 = !empty($agentTeamsCount[2]) ? $agentTeamsCount[2] : 0;
$level3 = !empty($agentTeamsCount[3]) ? $agentTeamsCount[3] : 0;

if ($_W['isajax']) {
	$pindex = max(1, intval($_GPC['page']));
	$psize = 20;
	$level = intval($_GPC['level']);
	$level = $level <= 3 && $level >= 1 ? $level : 1;

	$list = array();

	$agentTeams = $this->model->getCommissionTeam($openid, $level, $pindex, $psize);

	if (!empty($agentTeams)) {
		foreach ($agentTeams as &$row) {
			$recordPrice = $this->model->getCommissionRecordPrice($row['openid'], array('total'));
			$row['commission_total'] = !empty( $recordPrice['total'] ) ? number_format($recordPrice['total'], 2) : 0;

			$rowTeamsCount = $this->model->getCommissionTeamCount($row['openid'], $this->set['level']);
			$row['agentcount'] = !empty($rowTeamsCount['all']) ? intval($rowTeamsCount['all']) : 0;

			$row['agenttime'] = date('Y-m-d H:i', $row['agenttime']);
		}
	}
	unset($row);
	show_json(1, array('list' => $agentTeams, 'pagesize' => $psize));
}

include $this->template('team');